import { useCurd, useForm, useTable } from '@/hooks/useCurd'
import { PaginationDto } from '@/modules/common/common.dto'
import { Injectable } from 'butterfly-decorators'
import { ApplicationController } from '../application/application.controller'
import { Table, TableController } from './table.controller'

export const tableState = useTable<Table>()
export const formState = useForm<Table>()

export const init = () => {
  tableState.columns = [
    { type: 'selection' },
    { title: '表名', key: 'tableName' },
    { title: '表描述', key: 'tableDesc' }
  ]
  formState.template = [
    {
      type: 'select',
      label: '应用',
      keyPath: 'applicationId',
      selectProps: {
        options: [],
        placeholder: '请选择应用'
      }
    },
    {
      type: 'text',
      label: '表名',
      keyPath: 'tableName',
      textProps: { placeholder: '请输入表名（禁止中文）' }
    },
    {
      type: 'text',
      label: '表描述',
      keyPath: 'tableDesc',
      textProps: { placeholder: '请输入表描述' }
    }
  ]
}

@Injectable({
  controllers: [TableController, ApplicationController]
})
export class TableService {
  static tableController: TableController
  static applicationController: ApplicationController

  static async getApplicationList() {
    const { code, data } = await this.applicationController.findListReq()!
    if (code == 200) {
      formState.template?.forEach((item) =>
        item.keyPath === 'applicationId'
          ? (item.selectProps!.options = data.list.map((item) => ({
              label: item.applicationName,
              value: item.id
            })))
          : ''
      )
    }
  }

  static async getList({ page, size }: PaginationDto, ortherData?: any) {
    let res
    if (ortherData) {
      res = await this.tableController.findFilterReq({ page, size }, ortherData)!
    } else {
      res = await this.tableController.findListReq({ page, size })!
    }
    if (res.code === 200) {
      return { list: res.data.list, total: res.data.pageInfo.total }
    }
    return { list: [], total: 0 }
  }

  static async createOne(newData: Table) {
    const res = await this.tableController.addOneReq(newData)!
    if (res.code == 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async updateOne(newData: Table) {
    const res = await this.tableController.updateOneReq(newData)!
    if (res.code === 200) {
      return { success: true, msg: '操作成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }

  static async deleteByIds(ids: any[]) {
    const res = await this.tableController.deleteByIdsReq(ids)!
    if (res.code === 200) {
      return { success: true, msg: '删除成功' }
    } else {
      return { success: false, msg: res.message }
    }
  }
}

export const { r, c, u, d } = useCurd({
  r(page, size, ortherData?: any) {
    return TableService.getList({ page, size }, ortherData)
  },
  c(newData) {
    return TableService.createOne(newData)
  },
  u(newData) {
    return TableService.updateOne(newData)
  },
  d(keys) {
    return TableService.deleteByIds(keys)
  }
})
